package com.hay.elasticsearch.controller;

import com.hay.common.PageResult;
import com.hay.elasticsearch.pojo.Goods;
import com.hay.elasticsearch.pojo.SearchRequest;
import com.hay.elasticsearch.pojo.SearchResult;
import com.hay.elasticsearch.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @title: SearchController
 * @Author HuangYan
 * @Date: 2020/7/26 23:16
 * @Version 1.0
 */
@Controller
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 关键词查询
     * @param searchRequest
     * @return
     */
    @PostMapping("/page")
    public ResponseEntity<SearchResult> search(@RequestBody SearchRequest searchRequest){
        SearchResult result = searchService.searchByPage(searchRequest);
        if (result == null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
}
